/******************************************************************************
 * @File name   :      CardDefineServiceImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 12, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 12, 2015 1:39:56 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.card.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao;
import com.cap.bts.crmdp.dealer.card.domain.CardDefinition;
import com.cap.bts.crmdp.dealer.card.dto.CardDefineQueryDto;
import com.cap.bts.crmdp.dealer.card.dto.CardDefineSubmitDto;
import com.cap.bts.crmdp.dealer.card.service.ICardDefineService;
import com.cap.bts.crmdp.dealer.purchase.dao.IMemberPackageDao;
import com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class CardDefineServiceImpl implements ICardDefineService {

    @Resource(name = "cardDefineDao")
    private ICardDefineDao cardDefineDao;
    
    @Resource(name = "memberPackageDao")
    private IMemberPackageDao memberPackageDao;

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardDefineService#getCardDefinitionListByDto(com.cap.bts.crmdp.dealer.card.dto.CardDefineQueryDto)
     **/
    @Override
    public List<CardDefinition> getCardDefinitionListByDto(CardDefineQueryDto dto)
        throws AppException {
        
        List<CardDefinition> cardDefinitions=cardDefineDao.getCardDefinitionListByDto(dto);
        if (cardDefinitions!=null&&!cardDefinitions.isEmpty()) {
            for(CardDefinition cardDefinition:cardDefinitions){
                MemberPackageInfo maxMpi=memberPackageDao.getMaxMemberPackageInfoByPhysicalCardPrefix(cardDefinition.getPerfix());
                if(maxMpi!=null){
                    cardDefinition.setCardNum(maxMpi.getPackageNumber());
                }
            }
        }
        return cardDefinitions;
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardDefineService#updateCardDefinition(com.cap.bts.crmdp.dealer.card.dto.CardDefineSubmitDto)
     **/
    @Override
    public void updateCardDefinition(CardDefineSubmitDto dto) throws AppException {
        if (dto.getId() != null) {
            CardDefinition cd = cardDefineDao.findById(dto.getId());
            if (cd != null) {
                StringBuilder sb = new StringBuilder();
                if (dto.getIsPhysicalCard() != null) {
                    cd.setId(dto.getId());
                    if (dto.getPerfix() != null && !dto.getPerfix().isEmpty()) {
                        sb.append(dto.getPerfix());
                        cd.setPerfix(dto.getPerfix().toUpperCase());
                    } else {
                        sb.append("");
                        cd.setPerfix("");
                    }
                    cd.setIsPhysicalCard(dto.getIsPhysicalCard());
                    if (dto.getLengthOfDigital() != null && dto.getLengthOfDigital() != 0) {
                        sb.append(String
                            .format("%0" + dto.getLengthOfDigital().toString() + "d", 0));
                        cd.setLengthOfDigital(dto.getLengthOfDigital());
                    } else {
                        sb.append("");
                        cd.setLengthOfDigital(0);
                    }
                    if (dto.getCheckBitEnabeld() != null) {
                        sb.append("X");
                        cd.setCheckBitEnabeld(dto.getCheckBitEnabeld());
                    } else {
                        sb.append("");
                        cd.setCheckBitEnabeld(0);
                    }
                    cd.setActivationCodeLength(dto.getActivationCodeLength());
                    cd.setActivationCodeType(dto.getActivationCodeType());
                    cd.setStatus(dto.getStatus());
                }else{
                    if(dto.getPerfix().compareTo(cd.getPerfix())>0){
                        cd.setPerfix(dto.getPerfix());
                        sb.append(dto.getPerfix());
                        sb.append(String
                            .format("%0" + cd.getLengthOfDigital().toString() + "d", 0));
                        if (cd.getCheckBitEnabeld() != null) {
                            sb.append("X");
                        } else {
                            sb.append("");
                        }
                    }
                }
                
                if(dto.getCardNum()!=null&&!"".equals(dto.getCardNum())){
                    cd.setCardNum(dto.getCardNum());
                }else{
                    cd.setCardNum(sb.toString());
                }
                cd.setId(dto.getId());
                cd.setStatus(dto.getStatus());
                cardDefineDao.update(cd);
            }
        }
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardDefineService#addCardDefinition(com.cap.bts.crmdp.dealer.card.dto.CardDefineSubmitDto)
     **/
    @Override
    public CardDefinition addCardDefinition(CardDefineSubmitDto dto) throws AppException {
        CardDefinition cd = new CardDefinition();
        cd.setId(dto.getId());
        StringBuilder sb = new StringBuilder();
        if (dto.getPerfix() != null && !dto.getPerfix().isEmpty()) {
            sb.append(dto.getPerfix());
            cd.setPerfix(dto.getPerfix().toUpperCase());
        } else {
            sb.append("");
            cd.setPerfix("");
        }
        cd.setIsPhysicalCard(dto.getIsPhysicalCard());
        if (dto.getLengthOfDigital() != null && dto.getLengthOfDigital() != 0) {
            sb.append(String.format("%0" + dto.getLengthOfDigital().toString() + "d", 0));
            cd.setLengthOfDigital(dto.getLengthOfDigital());
        } else {
            sb.append("");
            cd.setLengthOfDigital(0);
        }
        if (dto.getCheckBitEnabeld() != null) {
            sb.append("X");
            cd.setCheckBitEnabeld(dto.getCheckBitEnabeld());
        } else {
            sb.append("");
            cd.setCheckBitEnabeld(0);
        }
        cd.setName(dto.getName());
        cd.setActivationCodeLength(dto.getActivationCodeLength());
        cd.setActivationCodeType(dto.getActivationCodeType());
        
        if(dto.getCardNum()!=null&&!"".equals(dto.getCardNum())){
            cd.setCardNum(dto.getCardNum());
            cd.setStatus(1);
        }else{
            cd.setStatus(dto.getStatus());
            cd.setCardNum(sb.toString());
        }
        return cardDefineDao.save(cd);
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardDefineService#deleteCardDefinition(com.cap.bts.crmdp.dealer.card.dto.CardDefineSubmitDto)
     **/
    @Override
    public void deleteCardDefinition(CardDefineSubmitDto dto) throws AppException {
        cardDefineDao.remove(dto.getId());
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Aug 25, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardDefineService#getCardDefinitionCount(com.cap.bts.crmdp.dealer.card.dto.CardDefineQueryDto)
     **/
    @Override
    public Integer getCardDefinitionCount(CardDefineQueryDto dto) throws AppException {
        return cardDefineDao.getCardDefinitionCount(dto);
    }

    /**
     * 查询所有的卡片信息
     * @see com.cap.bts.crmdp.dealer.card.service.ICardDefineService#getCardList()
     */
    @Override
    public List<CardDefinition> getCardList() throws AppException {
        return cardDefineDao.getCardList();
    }
}
